package be.gnx.fogo.application.model;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import be.gnx.fogo.application.util.HibernateUtil;

public class Sexe {

  private Long id;

  private char code;

  private String value;

  public static Sexe UNKNOWN;
  public static Sexe FEMALE;
  public static Sexe MALE;

  public static void initialise() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = session.getTransaction();
    transaction.begin();
    Query query = session.createQuery("from Sexe");
    query.setReadOnly(true);
    List<Sexe> sexes = query.list();
    HashMap<Character, Sexe> sexesHashMap = new HashMap<Character, Sexe>();
    for (Sexe sexe : sexes) {
      sexesHashMap.put(sexe.code, sexe);
    }
    if (!sexesHashMap.containsKey('U')) {
      UNKNOWN = new Sexe('U', "unknown");
      session.persist(UNKNOWN);
      session.save(UNKNOWN);
    } else {
      UNKNOWN = sexesHashMap.get('U');
    }
    if (!sexesHashMap.containsKey('F')) {
      FEMALE = new Sexe('F', "female");
      session.persist(FEMALE);
      session.save(FEMALE);
    } else {
      FEMALE = sexesHashMap.get('F');
    }
    if (!sexesHashMap.containsKey('M')) {
      MALE = new Sexe('M', "male");
      session.persist(MALE);
      session.save(MALE);
    } else {
      MALE = sexesHashMap.get('M');
    }
    transaction.commit();
  }

  public Sexe() {
    
  }

  private Sexe(char code, String value) {
    this.code = code;
    this.value= value;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public char getCode() {
    return code;
  }

  public void setCode(char code) {
    this.code = code;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

}
